package icraf.carbon.gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.RenderingHints;
import java.awt.Window;
import java.awt.Dialog.ModalityType;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.geom.Rectangle2D;

import javax.swing.BorderFactory;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;

class MoveMouseListener implements MouseListener, MouseMotionListener {
	public Window getFrame(Container target) {
		if (target instanceof Window) {
			return (Window) target;
		}
		return getFrame(target.getParent());
	}
	Point start_drag;
	Point start_loc;

	JComponent target;

	public MoveMouseListener(JComponent target) {
		this.target = target;
	}

	Point getScreenLocation(MouseEvent e) {
		Point cursor = e.getPoint();
		Point target_location = this.target.getLocationOnScreen();
		return new Point((int) (target_location.getX() + cursor.getX()),
				(int) (target_location.getY() + cursor.getY()));
	}

	public void mouseClicked(MouseEvent e) {
		Point cursor = e.getPoint();
		if((cursor.getY()<25)&&(cursor.getX()>target.getWidth()-25)) {
			Window frame = this.getFrame(target);
			frame.setVisible(false);
		}
	}

	public void mouseDragged(MouseEvent e) {
		Point current = this.getScreenLocation(e);
		Point offset = new Point(
				(int) current.getX() - (int) start_drag.getX(), (int) current
						.getY()
						- (int) start_drag.getY());
		Window frame = this.getFrame(target);
		Point new_location = new Point((int) (this.start_loc.getX() + offset
				.getX()), (int) (this.start_loc.getY() + offset.getY()));
		frame.setLocation(new_location);
	}

	public void mouseEntered(MouseEvent e) {
	}

	public void mouseExited(MouseEvent e) {
	}

	public void mouseMoved(MouseEvent e) {
	}

	public void mousePressed(MouseEvent e) {
		this.start_drag = this.getScreenLocation(e);
		this.start_loc = this.getFrame(this.target).getLocation();
	}

	public void mouseReleased(MouseEvent e) {
	}
}

public class Notes extends JPanel {
	private static final long serialVersionUID = -6267507992297896197L;

	public static void main(String[] args) {
		Notes n = new Notes();
		n.setText("<html>Ini adalah contoh teks dsfdks <br>test beda line <br> dsgdhfdhfdhfdj truytu <br> wqetrewt wetet ewtewt ewtewt <br> ret</html>");
		n.createDialog(null, null);
	}

	private JLabel label;

	public Notes() {
		init();
	}
	
	public Notes(String text) {
		init();
		setText(text);
	}
	
	public void init() {
		label = new JLabel();
		label.setHorizontalAlignment(JLabel.CENTER);
		label.setFont(getFont());
		this.setLayout(new BorderLayout());
		this.setBorder(BorderFactory.createEmptyBorder(4,4,4,4));
		this.add(label);
	}

	public void setText(String text){
		label.setText(text);
	}
	
	public void paintComponent(Graphics g) {
		Graphics2D g2 = (Graphics2D) g;
	    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
	        RenderingHints.VALUE_ANTIALIAS_ON);
	    Dimension dim = this.getSize();

	    GradientPaint gradBack2 = new GradientPaint(0, 0, new Color(255,255,240), dim.width, dim.height,
	    		new Color(255,255,180));
	    g2.setPaint(gradBack2);
	    g2.fill(new Rectangle2D.Double(0, 0, dim.width, dim.height));
	    
	    g2.setColor(Color.lightGray);
	    g2.drawLine(0, 0, 0, dim.height);
	    g2.drawLine(0, 0, dim.width, 0);
	    g2.setColor(Color.gray);
	    g2.drawLine(0, dim.height-1, dim.width-1, dim.height-1);
	    g2.drawLine(dim.width-1, 0, dim.width-1, dim.height-1);
	    g2.setColor(Color.white);
	    g2.drawRect(1, 1, dim.width-4, dim.height-4);
	    g2.setColor(Color.gray);
	    g2.drawString("X", dim.width-15, 15);
	}
	
	public JDialog createDialog(Window owner, ModalityType modalityType) {
		JDialog dialog = new JDialog(owner, modalityType);

		dialog.getContentPane().add(this);
		dialog.setUndecorated(true);

		MoveMouseListener mml = new MoveMouseListener(this);
		this.addMouseListener(mml);
		this.addMouseMotionListener(mml);

		dialog.pack();
		dialog.setVisible(true);
		return dialog;
	}

	public JLabel getLabel() {
		return label;
	}
}